package com.iotechn.unimall.admin.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {
    public  final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public String post(String postURL,String name,String age) throws Exception {
        NameValuePair[] data = {
                new NameValuePair("name", name),
                new NameValuePair("age", age)
        };
        return post(postURL,data);
    }

    /**
     * 执行一个带参数的HTTP POST请求，返回请求响应的JSON字符串
     *
     * @param url 请求的URL地址
     * @param map 请求的map参数
     * @return 返回请求响应的JSON字符串
     */
    public  String doPost(String url, Map<String, Object> map) throws Exception{
        // 这个basicNameValue**放在List中
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        // 创建basicNameValue***对象参数
        if (map != null) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                nameValuePairs.add(new NameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        // 填入各个表单域的值
        NameValuePair[] param = nameValuePairs.toArray(new NameValuePair[nameValuePairs.size()]);
        return post(url,param);
    }

    public String post(String postURL,NameValuePair[] data) throws Exception {
        logger.info("result======="+data);
        PostMethod postMethod = new PostMethod(postURL);
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        //参数设置，需要注意的就是里边不能传NULL，要传空字符串
        postMethod.setRequestBody(data);
        HttpClient httpClient = new HttpClient();
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        logger.info("result========================"+result);
        return result;
    }

    public  String doGet(String url, Map<String, Object> map) throws Exception{
        // 这个basicNameValue**放在List中
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        // 创建basicNameValue***对象参数
        if (map != null) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                nameValuePairs.add(new NameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        // 填入各个表单域的值
        NameValuePair[] param = nameValuePairs.toArray(new NameValuePair[nameValuePairs.size()]);
        return get(url,param);
    }

    public String get(String postURL,NameValuePair[] data) throws Exception {
        GetMethod getMethod = new GetMethod(postURL);
        getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        //参数设置，需要注意的就是里边不能传NULL，要传空字符串
        getMethod.setQueryString(data);
        HttpClient httpClient = new HttpClient();
        httpClient.executeMethod(getMethod);
        String result = getMethod.getResponseBodyAsString();
        return result;
    }
}
